/*
 * @(#)Login.java   1.0  2021年11月23日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swift.core.env.KeyValueConfig;
import com.swift.core.model.ServiceRequest;
import com.swift.core.model.data.DataModel;
import com.swift.core.service.SynInterface;
import com.swift.core.session.AbstractSession;
import com.swift.core.validator.DataValidator;
import com.swift.core.validator.annotation.ParamNotEmpty;
import com.swift.dts.loadconfig.UserConfig;
import com.swift.dts.service.core.UserCookie;
import com.swift.exception.extend.SystemException;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年11月23日
 */
@Service("dtsLogin")
public class DtsLogin implements SynInterface{
    
    @Autowired
    private UserConfig userConfig;
    
    @Autowired
    private UserCookie userCookie;
    /** 
     * @see com.swift.core.service.SynInterface#doService(com.swift.core.model.ServiceRequest)
     */
    @Override
    @DataValidator(NotEmpty = {
        @ParamNotEmpty(param = "username", message = "用户不能为空"),
        @ParamNotEmpty(param = "password", message = "密码不能为空"),
    })
    public DataModel doService(ServiceRequest req) {
        userConfig.refresh();
        String username = req.getData().getString("username");
        String password = req.getData().getString("password");
        String sysusername = "admin";
        String syspassword = "dtsadmin";
        KeyValueConfig userNameKey = userConfig.findConfig("userName");
        KeyValueConfig passwordKey = userConfig.findConfig("password");
        if(userNameKey!=null) sysusername=userNameKey.getValue();
        if(passwordKey!=null) syspassword=passwordKey.getValue();
        if(!username.equals(sysusername)) throw new SystemException("登录异常：帐号不正常");
        if(!password.equals(syspassword)) throw new SystemException("登录异常：密码错误");
        AbstractSession sessionUser = new AbstractSession();
        sessionUser.setUserName(username);
        userCookie.addSSoCookie(req, sessionUser);
        return null;
    }

}
